package com.hellogwt.client.view;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.HasClickHandlers;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.Widget;
import com.hellogwt.client.presenter.GreetingsPresenter;
import com.hellogwt.shared.domain.Greeting;

import java.util.List;

public class GreetingsView extends Composite implements GreetingsPresenter.Display {

    interface GreetingsViewUiBinder extends UiBinder<Widget, GreetingsView> {
    }

    private static GreetingsViewUiBinder uiBinder = GWT.create(GreetingsViewUiBinder.class);

    @UiField
    Button editButton;
    @UiField
    FlexTable greetingsFlexTable;

    public GreetingsView() {
        initWidget(uiBinder.createAndBindUi(this));
    }

    @Override
    public HasClickHandlers getEditButton() {
        return editButton;
    }

    @Override
    public void setData(List<Greeting> data) {
        greetingsFlexTable.removeAllRows();

        greetingsFlexTable.setText(0, 0, "Author");
        greetingsFlexTable.setText(0, 1, "Text");

        for (Greeting greeting : data) {
            int row = greetingsFlexTable.getRowCount();

            greetingsFlexTable.setText(row, 0, greeting.getAuthor());
            greetingsFlexTable.setText(row, 1, greeting.getText());
        }
    }

    @Override
    public Widget asWidget() {
        return this;
    }
}